package com.phonetools.commonutils

/**
 * 泛型实例化工具
 * */

/*
fun button(text: String, block: JButton.() -> Unit) = new<JButton>(text).also(block)
fun main(args: Array<String>) {
    button("Click") {
        addActionListener { ... }
    }
}
*/

/**
 * 实例化任意类型，不带构造参数
 * */
inline fun <reified T : Any> new(): T {
    val clz = T::class.java
    val mCreate = clz.getDeclaredConstructor()
    mCreate.isAccessible = true
    return mCreate.newInstance()
}

/**
 * 实例化任意类型，不带构造参数
 * */
inline fun <reified T : Any> new(vararg params: Any): T {
    val clz = T::class.java
    val paramTypes = params.map { it::class.java }.toTypedArray()
    val mCreate = clz.getDeclaredConstructor(*paramTypes)
    mCreate.isAccessible = true
    return mCreate.newInstance(* params)
}